//
//  AY_BookSettingCell.m
//  AiYue
//
//  Created by boWen on 2023/10/19.
//

#import "AY_BookSettingCell.h"
#import "AY_BookMSwitch.h"

@interface AY_BookSettingCell ()
@property (nonatomic, strong) UIView * bg_View;
@property (nonatomic, strong) UILabel * leftTopLabel;
@property (nonatomic, strong) UILabel * leftDownLabel;
@property (nonatomic, strong) UILabel * rightLabel;
@property (nonatomic, strong) UIImageView * arrowImgView;
@property (nonatomic, strong) UIImageView * deleteImgView;
@property (nonatomic, strong) UIImageView * avatarImgView;
@property (nonatomic, strong) NSArray * dataLeftAry;
@property (nonatomic, strong) NSArray * dataLeftSubAry;
@property (nonatomic, strong) AY_BookMSwitch * yejianSwitch;
@property (nonatomic, strong) AY_BookMSwitch * chapteSwitch;
@property (nonatomic, strong) UIView *huYanView;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation AY_BookSettingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}

- (void)addAllSubviewsWithUI {
    if ([AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        self.dataLeftAry = @[@"个人信息", @"夜间模式", @"清除缓存", @"检查更新"];
        self.dataLeftSubAry = @[@"资料修改", @"开启夜间模式", @"点击清理缓存", @"当前版本1.0"];
    }else{
        self.dataLeftAry = @[@"个人信息", @"夜间模式", [NSString stringWithFormat:@"自动%@章节", [AYMMKV getStringForKey:GM]], @"清除缓存", @"检查更新"];
        self.dataLeftSubAry = @[@"资料修改", @"开启夜间模式", [AYMMKV getBoolForKey:AUTO defaultValue:YES] ? @"当前打开" : @"当前关闭", @"点击清理缓存", @"当前版本1.0"];
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(0) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
    self.leftTopLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(16) weight:UIFontWeightRegular] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.leftTopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bg_View).offset(YNUM(-8));
        make.left.equalTo(self.bg_View).offset(YNUM(14));
        make.height.mas_equalTo(YNUM(22));
    }];
    
    self.leftDownLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#909090") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(12) weight:UIFontWeightRegular] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.leftDownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftTopLabel);
        make.top.equalTo(self.leftTopLabel.mas_bottom).offset(YNUM(3));
        make.height.mas_equalTo(YNUM(16));
    }];
    self.arrowImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_arrowNight.png") andBg:[UIColor whiteColor] andRadius:0.0 andAddSubView:self.bg_View];
    [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(16));
        make.width.mas_equalTo(YNUM(16));
        make.right.equalTo(self.bg_View).offset(YNUM(-14));
        make.centerY.equalTo(self.bg_View);
    }];
    
    self.avatarImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_tx.png") andBg:[UIColor whiteColor] andRadius:22.5 andAddSubView:self.bg_View];
    [self.avatarImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(45));
        make.width.mas_equalTo(YNUM(45));
        make.right.equalTo(self.arrowImgView.mas_left).offset(YNUM(-5));
        make.centerY.equalTo(self.bg_View);
    }];
    
    self.deleteImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_delete.png") andBg:[UIColor whiteColor] andRadius:0.0 andAddSubView:self.bg_View];
    [self.deleteImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(15));
        make.width.mas_equalTo(YNUM(15));
        make.right.equalTo(self.bg_View).offset(YNUM(-14));
        make.centerY.equalTo(self.bg_View);
    }];
    
    self.rightLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#BFBFBF") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bg_View);
        make.right.equalTo(self.arrowImgView.mas_left).offset(YNUM(-4));
        make.height.mas_equalTo(YNUM(22));
    }];
    
    self.yejianSwitch = [[AY_BookMSwitch alloc] initWithFrame:CGRectMake(SCREEN_W - YNUM(66), YNUM(18), YNUM(50), YNUM(30))];
    [self.yejianSwitch setOnTintColor:THEME_COLOR];
    AY_ReadManager *readManager = [AY_ReadManager sharedInstance];
    [self.yejianSwitch setOn:readManager.isNight
                     animated:YES];
    [self.bg_View addSubview:self.yejianSwitch];
    Weak_Self
    [self.yejianSwitch setDidChangeHandler:^(BOOL isOn) {
        readManager.isNight = isOn;
        if (isOn) {
            [[AYTool lastWindow] addSubview:weakSelf.huYanView];
        }else {
            for (int i = 0; i < [[AYTool lastWindow].subviews count]; i ++) {
                UIView *view = [AYTool lastWindow].subviews[i];
                if (view.tag == 263476) {
                    [view removeFromSuperview];
                }
            }
        }
    }];
    
    self.chapteSwitch = [[AY_BookMSwitch alloc] initWithFrame:CGRectMake(SCREEN_W - YNUM(66), YNUM(18), YNUM(50), YNUM(30))];
    [self.chapteSwitch setOnTintColor:THEME_COLOR];
    [self.chapteSwitch setOn:[AYMMKV getBoolForKey:AUTO defaultValue:YES]
                     animated:YES];
    [self.bg_View addSubview:self.chapteSwitch];
    [self.chapteSwitch setDidChangeHandler:^(BOOL isOn) {
        [AYMMKV setBool:isOn forKey:AUTO];
        self.leftDownLabel.text = [AYMMKV getBoolForKey:AUTO defaultValue:YES] ? @"当前打开" : @"当前关闭";
    }];
}

- (void)setData:(NSIndexPath *)index {
    _indexPath = index;
    if(index.section == 0) {
      self.deleteImgView.hidden = YES;
      self.arrowImgView.hidden = NO;
        self.avatarImgView.hidden = NO;
      self.rightLabel.hidden = YES;
      self.yejianSwitch.hidden = YES;
      self.chapteSwitch.hidden = YES;
      self.leftTopLabel.text = self.dataLeftAry[index.row];
      self.leftDownLabel.text = self.dataLeftSubAry[index.row];
    }else {
        self.leftTopLabel.text = self.dataLeftAry[index.row + 1];
        self.leftDownLabel.text = self.dataLeftSubAry[index.row + 1];
        self.avatarImgView.hidden = YES;
        if (index.row == 0) {
           self.yejianSwitch.hidden = NO;
        }else {
            self.yejianSwitch.hidden = YES;
        }
        if (index.row == 1&&[self.leftTopLabel.text isEqualToString:[NSString stringWithFormat:@"自动%@章节", [AYMMKV getStringForKey:GM]]]) {
            self.chapteSwitch.hidden = NO;
        }else {
            self.chapteSwitch.hidden = YES;
        }
        if (index.row == 2) {
            self.rightLabel.hidden = NO;
            self.deleteImgView.hidden = NO;
        }else {
            self.rightLabel.hidden = YES;
            self.deleteImgView.hidden = YES;
        }
        if (index.row == 3) {
            self.arrowImgView.hidden = NO;
        }else {
            self.arrowImgView.hidden = YES;
        }
    }
    
    
  
}

- (UIView *)huYanView
{
    if (!_huYanView) {
        _huYanView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _huYanView.backgroundColor = [UIColor blackColor];
        _huYanView.alpha = 0.3;
        _huYanView.userInteractionEnabled = NO;
        _huYanView.tag = 263476;
    }
    return _huYanView;
}

@end
